﻿using GreenXi.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Controls;

namespace GreenXi.ViewModel
{
    class IOViewModel : INotifyPropertyChanged
    {
        public IOViewModel()
        {
            IO ??= new IO()
            {
                //输入
                Input = new List<string>(){
                    "点动-", "点动+", "开始", "停止",
                    "回机械原点", "机械寻零", "单步", "PLC安全锁",
                    "运行模式", "设置模式", "程序位5", "程序位4",
                    "程序位3", "程序位2", "程序位1", "程序位0"
                },
                //输出
                Output = new List<string>(){
                    "OK", "NOK", "运行","空" ,
                    "准备", "设置模式", "系统正常", "空" ,
                    "工作/机械原点", "机械原点完成", "同服准备", "伺服升",
                    "心跳", "PC连接", "PLC安全锁", "本地安全锁"
                },
                //数字变量
                NumericVariable = new List<string>(){
                    "0.00", "0.00", "0.00", "0.00",
                    "0.00", "0.00", "0.00", "0.00",
                    "0.00", "0.00", "0.00", "0.00",
                    "0.00", "0.00", "0.00", "0.00",
                    "0.00", "0.00", "0.00", "0.00",
                },
                Tag = new List<string>(){
                    "0", "1", "2", "3",
                    "4","5", "6", "7",
                    "8","9", "10", "11",
                    "12","13", "14", "15",
                    "M0","M1", "M2", "M3",
                },
                //输入变量
                InputVariable = new List<string>(){
                    "输入0", "输入4", "输入8", "输入12",
                    "输入1", "输入5", "输入9", "输入13",
                    "输入2", "输入6", "输入10", "输入14",
                    "输入3", "输入7", "输入11", "输入15"
                },
                //输出变量
                OutputVariable = new List<string>(){
                    "输入0", "输入4", "输入8", "输入12",
                    "输入1", "输入5", "输入9", "输入13",
                    "输入2", "输入6", "输入10", "输入14",
                    "输入3", "输入7", "输入11", "输入15"
                },
            };
        }

        #region IO对象数据绑定
        private static IO? _IO;
        public IO? IO
        {
            get => _IO;
            set
            {
                _IO = value;
                OnPropertyChanged(nameof(IO));
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region 根据绑定列表名生成按钮
        /// <summary>
        /// 根据绑定列表生成按钮
        /// </summary>
        /// <param name="grid">指定Grid名</param>
        /// <param name="name">绑定的列表名</param>
        public static void GenerateButtons(Grid grid, string name)
        {
            // 根据传入的列表名获取按钮绑定列表
            string[,] buttonBindings = name switch
            {
                "Input" => GenerateButtonBindings_Input(),
                "Output" => GenerateButtonBindings_Output(),
                "InputVariable" => GenerateButtonBindings_InputVariable(),
                "OutputVariable" => GenerateButtonBindings_OutputVariable(),
                _ => throw new ArgumentException("绑定的列表名无效", nameof(name)),
            } ?? throw new InvalidOperationException($"无法生成按钮绑定列表 {name}");

            for (int i = 0; i < buttonBindings.GetLength(0); i++)
            {
                int row = i;
                for (int j = 0; j < buttonBindings.GetLength(1); j++)
                {
                    int column = j;

                    // 创建一个新的 Button 控件，并设置其属性
                    var button = new Button
                    {
                        Width = 100,
                        Height = 65,
                        Content = buttonBindings[i, j]
                    };

                    // 将 Button 控件添加到指定的 Grid 中的指定行和列
                    Grid.SetRow(button, row);
                    Grid.SetColumn(button, column);
                    grid.Children.Add(button);
                }
            }
        }
        #endregion

        #region 生成按钮的绑定列表
        public static string[,] GenerateButtonBindings_Input()
        {
            string[,] buttonBindings = new string[4, 4];

            for (int row = 0; row < 4; row++)
            {
                for (int column = 0; column < 4; column++)
                {
                    int inputIndex = row * 4 + column;
                    buttonBindings[row, column] = _IO?.Input?[inputIndex] ?? "数据为null";
                }
            }
            return buttonBindings;
        }

        public static string[,] GenerateButtonBindings_Output()
        {
            string[,] buttonBindings = new string[4, 4];

            for (int row = 0; row < 4; row++)
            {
                for (int column = 0; column < 4; column++)
                {
                    int outputIndex = row * 4 + column;
                    buttonBindings[row, column] = _IO?.Output?[outputIndex] ?? "数据为null";
                }
            }
            return buttonBindings;
        }




        public static string[,] GenerateButtonBindings_InputVariable()
        {
            string[,] buttonBindings = new string[4, 4];

            for (int row = 0; row < 4; row++)
            {
                for (int column = 0; column < 4; column++)
                {
                    int inputVariableIndex = row * 4 + column;
                    buttonBindings[row, column] = _IO?.InputVariable?[inputVariableIndex] ?? "数据为null";
                }
            }
            return buttonBindings;
        }

        public static string[,] GenerateButtonBindings_OutputVariable()
        {
            string[,] buttonBindings = new string[4, 4];

            for (int row = 0; row < 4; row++)
            {
                for (int column = 0; column < 4; column++)
                {
                    int outputVariableIndex = row * 4 + column;
                    buttonBindings[row, column] = _IO?.OutputVariable?[outputVariableIndex] ?? "数据为null";
                }
            }
            return buttonBindings;
        }
        #endregion

        #region 生成materialDesign:Badged的绑定列表
        public static string[,] GenerateBadged_NumericVariable()
        {
            string[,] buttonBindings = new string[5, 4];

            for (int row = 0; row < 4; row++)
            {
                for (int column = 0; column < 4; column++)
                {
                    int numericVariableIndex = row * 4 + column;
                    buttonBindings[row, column] = _IO?.NumericVariable?[numericVariableIndex] ?? "数据为null";
                }
            }
            return buttonBindings;
        }


        public static string[,] GenerateBadgedBindings_Tag()
        {
            string[,] buttonBindings = new string[5, 4];

            for (int row = 0; row < 4; row++)
            {
                for (int column = 0; column < 4; column++)
                {
                    int tagIndex = row * 4 + column;
                    buttonBindings[row, column] = _IO?.Tag?[tagIndex] ?? "数据为null";
                }
            }
            return buttonBindings;
        }
        #endregion

    }
}
